//
//  ViewController.swift
//  IMGchange
//
//  Created by gaoyi on 2017/10/18.
//  Copyright © 2017年 gaoyi. All rights reserved.
//

import UIKit

class ViewController: UIViewController {
    var index:Int! = 0
    lazy var allArr: NSMutableArray = {
        () -> NSMutableArray in
        let tempallArr = NSMutableArray()
        let strPath = Bundle.main.path(forResource:"picList",ofType: "plist")
        let arr:NSArray = NSArray(contentsOfFile: strPath!)!
        for dict in arr {
            let model:singerModel = singerModel.SingerModelWithDict(_dict: dict as! NSDictionary)
            tempallArr.add(model)
            
        }
        return tempallArr
        
    }()
    lazy var titleLabel: UILabel = {
        () -> UILabel in
        let temptitleLabel = UILabel(frame: CGRect(x:60,y:60,width:200,height:50))
        temptitleLabel.backgroundColor = UIColor.red
        self.view.addSubview(temptitleLabel)
        return temptitleLabel
    }()
    lazy var imgView: UIImageView = {
        () -> UIImageView in
        let tempimgView = UIImageView(frame: CGRect(x:60,y:120,width:200,height:200))
        tempimgView.backgroundColor = UIColor.red
        self.view.addSubview(tempimgView)
        return tempimgView
    }()
    lazy var btnBack: UIButton = {
        () -> UIButton in
        let tempbtnBack = UIButton(frame: CGRect(x:60,y:330,width:95,height:50))
        tempbtnBack.setTitle("上一张", for: UIControlState.normal)
        tempbtnBack.addTarget(self, action:#selector(setBack), for: UIControlEvents.touchUpInside)
        self.view.addSubview(tempbtnBack)
        return tempbtnBack
    }()
    lazy var btnNext: UIButton = {
        () -> UIButton in
        let tempbtnNext = UIButton(frame: CGRect(x:165,y:330,width:95,height:50))
        tempbtnNext.setTitle("下一张", for: UIControlState.normal)
        tempbtnNext.addTarget(self, action: #selector(setNext), for: UIControlEvents.touchUpInside)
        self.view.addSubview(tempbtnNext)
        return tempbtnNext
    }()
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        self.showCurrentPage(iPage: self.index)
    }
    @objc func setBack(){
        if self.index > 0 && self.index <= self.allArr.count -
            1{
            self.index = self.index - 1
            let model:singerModel = self.allArr[self.index]
                as! singerModel
            let titleStr = NSString(string:"\(model.name!)\(self.index+1)/\(self.allArr.count) ")
            self.titleLabel.text = titleStr as String
            self.titleLabel.textAlignment = .center
            self.imgView.image = UIImage(named:model.pic as String)
            self.btnNext.isEnabled = true
            self.btnNext.backgroundColor = UIColor.yellow
            if self.index == 0{
                self.btnBack.isEnabled = false
                self.btnBack.backgroundColor = UIColor.gray
            }
        }
    }
    @objc func setNext(){
        if self.index >= 0 && self.index < self.allArr.count -
            1{
            self.index = self.index + 1
            let model:singerModel = self.allArr[self.index]
                as! singerModel
            let titleStr = NSString(string: "\(model.name!)\(self.index-1)/\(self.allArr.count) ")
            self.titleLabel.text = titleStr as String
            self.titleLabel.textAlignment = .center
            self.imgView.image = UIImage(named:model.pic as
                String)
            self.btnBack.isEnabled = true
            self.btnBack.backgroundColor = UIColor.yellow
            if self.index == 0{
                self.btnNext.isEnabled = false
                self.btnNext.backgroundColor = UIColor.gray
            }
        }
    }
    func showCurrentPage(iPage:Int){
        let model:singerModel = self.allArr[iPage] as!
        singerModel
        let titleStr = NSString(string: "\(model.name!)\(self.index+1)/\(self.allArr.count) ")
        self.titleLabel.text = titleStr as String
        self.titleLabel.textAlignment = .center
        self.imgView.image = UIImage(named:model.pic as String)
        self.btnBack.backgroundColor = UIColor.blue
        self.btnNext.backgroundColor = UIColor.blue
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    
}

